package com.whfc.ms.api.fmam.service.impl;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.fmam.entity.dto.FmamSupplierDTO;
import com.whfc.fmam.entity.qo.SupplierAddQO;
import com.whfc.fmam.service.FmamSupplierService;
import com.whfc.fuum.dto.AppCorpDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.param.AppCorpListQO;
import com.whfc.fuum.service.AppCorpService;
import com.whfc.ms.api.fmam.cache.LocalSysDeptService;
import com.whfc.ms.api.fmam.service.SupplierService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: ；likang
 * @Description:
 * @Date:Create：in 2019/11/14 18:35
 * @Version：1.0
 */
@Service
public class SupplierServiceImpl implements SupplierService {


    @DubboReference(interfaceClass = FmamSupplierService.class, version = "1.0.0")
    private FmamSupplierService fmamSupplierService;

    @DubboReference(interfaceClass = AppCorpService.class, version = "1.0.0")
    private AppCorpService appCorpService;

    @Autowired
    private LocalSysDeptService localSysDeptService;

    /**
     * 供/收料单位列表
     *
     * @param user
     * @param pageNum
     * @param pageSize
     * @param category
     * @param deptId
     * @return
     */
    @Override
    public PageData<FmamSupplierDTO> listFmamSupplierDTO(SysUser user, Integer pageNum, Integer pageSize, Integer category, Integer deptId) {
        PageData<FmamSupplierDTO> pageData = fmamSupplierService.listFmamSupplierDTO(pageNum, pageSize, category, deptId);
        List<FmamSupplierDTO> list = pageData.getList();

        for (FmamSupplierDTO fmamSupplierDTO : list) {
            //添加组织机构名称
            Integer deptId1 = fmamSupplierDTO.getDeptId();
            String deptName = localSysDeptService.getDeptName(deptId1);
            fmamSupplierDTO.setDeptName(deptName);
            //添加经营范围
            Integer corpId = fmamSupplierDTO.getCorpId();
            AppCorpDTO appCorpDTO = appCorpService.detail(corpId);
            if (appCorpDTO != null) {
                fmamSupplierDTO.setBizScope(appCorpDTO.getBizScope());
            }
        }
        return pageData;
    }

    @Override
    public ListData<FmamSupplierDTO> list(Integer deptId, Integer category) {
        ListData<FmamSupplierDTO> listData = fmamSupplierService.list(deptId, category);
        return listData;
    }

    /**
     * 查看单位详情
     *
     * @param id
     */
    @Override
    public FmamSupplierDTO supplierDetail(Integer id) {
        FmamSupplierDTO fmamSupplierDTO = fmamSupplierService.getFmamSupplierDetail(id);
        //补充单位详情信息
        Integer corpId = fmamSupplierDTO.getCorpId();
        AppCorpDTO appCorpDTO = appCorpService.detail(corpId);
        if (appCorpDTO != null) {
            fmamSupplierDTO.setProvince(appCorpDTO.getProvince());
            fmamSupplierDTO.setCity(appCorpDTO.getCity());
            fmamSupplierDTO.setArea(appCorpDTO.getArea());
            fmamSupplierDTO.setAddress(appCorpDTO.getAddress());
            fmamSupplierDTO.setBizScope(appCorpDTO.getBizScope());
        }
        return fmamSupplierDTO;
    }

    /**
     * 删除供/收料单位
     *
     * @param id
     */
    @Override
    public void deleteSupplier(Integer id) {
        fmamSupplierService.delSupplier(id);
    }


    /**
     * 选择合作单位列表（去重）
     *
     * @param user
     * @param request
     * @return
     */
    @Override
    public PageData<AppCorpDTO> listCorp(SysUser user, AppCorpListQO request) {
        //获取组织机构ID
        Integer deptId = request.getDeptId();
        //获取供应商类别
        Integer category = request.getCategory();
        //通过供应商类别及组织机构ID查询数据库已有的合作单位ID
        List<Integer> corpIds = fmamSupplierService.getCorpIds(category, deptId);
        request.setCorpIds(corpIds);
        PageData<AppCorpDTO> appCorpDTOPageData = appCorpService.list(request);
        return appCorpDTOPageData;
    }


    /**
     * 添加物资合作单位
     *
     * @param category
     * @param corpIds
     * @return
     */
    @Override
    public void addSupplier(Integer category, List<Integer> corpIds) {
        //判断合作单位ID数组非null
        if (corpIds != null && corpIds.size() > 0) {
            //循环遍历,根据合作单位ID查询表记录
            for (int i = 0; i < corpIds.size(); i++) {
                //从物资合作商数据库中查询记录是否存在
                Integer corpId = corpIds.get(i);
                FmamSupplierDTO fmamSupplierDTO = fmamSupplierService.getSupplierByCorpIdAndCategory(corpId, category);
                //判断物资合作社表记录,如为null,数据库中不存在
                if (fmamSupplierDTO == null) {
                    //查询合作单位记录，并新增到物资合作商
                    AppCorpDTO appCorpDTO = appCorpService.detail(corpId);
                    if (appCorpDTO != null) {
                        SupplierAddQO supplierAddQO = new SupplierAddQO();
                        supplierAddQO.setDeptId(appCorpDTO.getDeptId());
                        //添加合作单位类别;
                        supplierAddQO.setCategory(category);
                        supplierAddQO.setCode(appCorpDTO.getCorpCode());
                        supplierAddQO.setName(appCorpDTO.getCorpName());
                        supplierAddQO.setCorpId(appCorpDTO.getCorpId());
                        supplierAddQO.setCorpTypeId(appCorpDTO.getCorpTypeId());
                        // 合作单位类型名称
                        supplierAddQO.setCorpTypeName(appCorpDTO.getCorpTypeName());
                        supplierAddQO.setLinkPerson(appCorpDTO.getLegalMan());
                        supplierAddQO.setLinkPhone(appCorpDTO.getLegalManPhone());
                        supplierAddQO.setAddress(appCorpDTO.getAddress());
                        fmamSupplierService.addSupplier(supplierAddQO);
                    }
                }
            }
        }
    }

    @Override
    public void deleteBatch(List<Integer> idList) {
        fmamSupplierService.deleteBatch(idList);
    }

}
